package main

import (
	"fmt"
	"math/rand"
	"time"
)

func boring(name string) chan string {
	out := make(chan string)
	go func() {
		for i := 0; i < 10; i++ {
			out <- fmt.Sprintf("%s say: %d", name, i)
			time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
		}
		close(out)
	}()

	return out
}

func FanIn(b1, b2 chan string) chan string {
	t := make(chan string)
	go func() {
		for {
			t <- <-b1
		}
	}()
	go func() {
		for {
			t <- <-b2
		}
	}()
	return t
}

func main() {
	fadIn := FanIn(boring("jack"), boring("rose"))
	for i := 0; i < 20; i++ {
		fmt.Println(<-fadIn)
	}
	fmt.Println("you're both boring")
}
